package cn.pconline.autoclub.web.admin;

import cn.pconline.autoclub.entity.ClubIntegral;
import cn.pconline.autoclub.entity.ClubLevel;
import cn.pconline.autoclub.service.ClubLevelService;
import cn.pconline.autoclub.service.ClubUpgradePrizeService;
import cn.pconline.autoclub.service.PetrolService;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.gelivable.log.GeliLogFacade;
import org.gelivable.web.Env;
import org.gelivable.web.EnvUtils;
import org.gelivable.web.HttpMethod;
import org.gelivable.webmvc.JSONBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/admin")
public class LevelConfigurationController {

    @Autowired
    private PetrolService petrolService;
    @Autowired
    private ClubLevelService clubLevelService;
    @Autowired
    private ClubUpgradePrizeService prizeService;
    private static final Logger log = LoggerFactory.getLogger(LevelConfigurationController.class);

    private void successMsg(String msg, String navTabId, HttpServletResponse resp) {
        try {
            resp.setCharacterEncoding("UTF-8");
            resp.getWriter().println(new JSONBuilder()
                    .put("statusCode", 200)
                    .put("message", msg)
                    .put("navTabId", navTabId)
                    .toString());
        } catch (IOException ex) {
            log.error("set successMsg fail!" , ex);
        }
    }

    private void errorMsg(String msg, HttpServletResponse resp) {
        try {
            resp.setCharacterEncoding("UTF-8");
            resp.getWriter().println(new JSONBuilder()
                    .put("statusCode", 300)
                    .put("message", msg)
                    .toString());
        } catch (IOException ex) {
            log.error("set errorMsg fail!" , ex);
        }
    }

    /**
     * 车友会等级、积分配置列表
     *
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "/club/levelConfiguration.do")
    public String list(HttpServletRequest req, HttpServletResponse resp) {
        // 积分配置列表
        req.setAttribute("integrals", petrolService.getIntegrals());

        // 车友会等级列表
//        req.setAttribute("levels", clubLevelService.getSortLevels());

        return "admin/system/levelConfiguration";
    }

    /**
     * 添加积分配置
     *
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "/club/addIntegral.do")
    public String addIntegral(HttpServletRequest req, HttpServletResponse resp) {
        Env env = EnvUtils.getEnv();
        HttpMethod method = env.getHttpMethod();
        if(!method.equals(HttpMethod.POST)){
        	errorMsg("非法请求",resp);
        	return null;
        }
        String name = env.param("name", "");
        int petrol = env.paramInt("petrol", 0);

        ClubIntegral integral = new ClubIntegral();
        integral.setName(name);
        integral.setPetrol(petrol);
        petrolService.createOrUpdate(integral);

        this.successMsg("添加成功", "level-configuration", resp);

        return null;
    }

    /**
     * 添加车友会等级
     *
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "/club/addLevel.do")
    public String addLevel(HttpServletRequest req, HttpServletResponse resp) {
        Env env = EnvUtils.getEnv();
        HttpMethod method = env.getHttpMethod();
        if(!method.equals(HttpMethod.POST)){
        	errorMsg("非法请求",resp);
        	return null;
        }
        String levelName = env.param("levelName", "");
        int levelPetrol = env.paramInt("levelPetrol", 0);
        int levelMember = env.paramInt("levelMember", 0);

        ClubLevel clubLevel = new ClubLevel();
        clubLevel.setLevelName(levelName);
        clubLevel.setLevelPetrol(levelPetrol);
        clubLevel.setLevelMember(levelMember);
        long id = clubLevelService.create(clubLevel);

        // 添加奖品
        prizeService.autoInitPrize(id);

        this.successMsg("添加成功", "level-configuration", resp);

        return null;
    }

    /**
     * 修改积分配置
     *
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "/club/updateIntegral.do")
    public String updateIntegral(HttpServletRequest req, HttpServletResponse resp) {
        Env env = EnvUtils.getEnv();
        GeliLogFacade.log();
        HttpMethod method = env.getHttpMethod();
        if(!method.equals(HttpMethod.POST)){
        	errorMsg("非法请求",resp);
        	return null;
        }
        long integralId = env.paramLong("integralId", 0);
        String name = env.param("name", "");
        int petrol = env.paramInt("petrol", 0);

        ClubIntegral integral = petrolService.getById(integralId);
        if (integral == null) {
            this.errorMsg("数据异常，修改失败", resp);
            log.error("updateIntegral: 数据异常，不存在该积分配置，修改失败");
            return null;
        }

        integral.setName(name);
        integral.setPetrol(petrol);
        petrolService.createOrUpdate(integral);

        this.successMsg("修改成功", "level-configuration", resp);

        return null;
    }

    /**
     * 修改车友会等级
     *
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "/club/updateLevel.do")
    public String updateLevel(HttpServletRequest req, HttpServletResponse resp) {
        Env env = EnvUtils.getEnv();
        HttpMethod method = env.getHttpMethod();
        if(!method.equals(HttpMethod.POST)){
        	errorMsg("非法请求",resp);
        	return null;
        }
        long levelId = env.paramLong("levelId", 0);
        String levelName = env.param("levelName", "");
        int levelPetrol = env.paramInt("levelPetrol", 0);
        int levelMember = env.paramInt("levelMember", 0);

        ClubLevel clubLevel = clubLevelService.getById(levelId);
        if (clubLevel == null) {
            this.errorMsg("数据异常，修改失败", resp);
            return null;
        }

        clubLevel.setLevelName(levelName);
        clubLevel.setLevelPetrol(levelPetrol);
        clubLevel.setLevelMember(levelMember);
        clubLevelService.update(clubLevel);

        this.successMsg("修改成功", "level-configuration", resp);

        return null;
    }
}
